const {
	db,
	dbCmd,
	result_success,
	result_error,
	appletInfoCollection,
	collectionCollection
} = require('../../common/constants')
module.exports = async function(params = {}) {
	const token = this.getUniIdToken()
	if (!token) {
		return result_error('登录失效,请重新登录')
	}
	let payload = await this.uniID.checkToken(token)
	if (payload.code) {
		return payload
	}
	let type = params.type
	let tCollectionData = params.collectionData
	/* if(type==='alipay'){
		return result_error('赞不支持')
	} */
	let id = params.id
	let collectionData = await collectionCollection.doc(id)
		.get()
	collectionData = collectionData.data[0]
	let res = await db.collection('wmzmall-collection-share').add({
		collectionId: id,
		type: params.type,
		create_time: new Date().getTime(),
		validity: collectionData.validity,
		share_user_id: payload.uid
	})
	if (!tCollectionData) {
		let title = collectionData.name + (collectionData.money / 100).toFixed(2) + "元,待支付"
		if (collectionData.type === 1) {
			title = collectionData.name + ",待支付"
		}
		let miniId = collectionData.wechat_gh_id
		let alipay_id = collectionData.alipay_id
		let path = 'pages/collection/collection?collectionId=' + res.id
		let imageUrl =
			'https://7463-tcb-fy6f7f4nm7mhifi-6c366c810511-1308293149.tcb.qcloud.la/fukuan/20230612160500.png'
		let shareRes = await db.collection('wmzmall-app-share').where({
			shop_id: collectionData.shop_id
		}).get()
		shareRes = shareRes.data[0]
		if (shareRes && shareRes.collection_img) {
			imageUrl = shareRes.collection_img
		}
		let webUrl = "https://mall.web.wmeizi.com/skipapplet.html?appId=" + alipay_id + "&collectionId=" + res
			.id
		let summary = ''
		if (type === 'wechat_applet') {
			return result_success({
				miniId: collectionData.wechat_id,
				path: 'pages/collection/collection-share?collectionId=' + res.id
			})
		}
		return result_success({
			title: title,
			miniId: miniId,
			path: path,
			imageUrl: imageUrl,
			webUrl: webUrl,
			summary: summary
		})
	} else {
		if (collectionData.share_title != tCollectionData.share_title || collectionData.share_image !=
			tCollectionData.share_image || collectionData.share_summary != tCollectionData.share_summary) {
			await collectionCollection.doc(id).update({
				share_title: tCollectionData.share_title,
				share_image: tCollectionData.share_image,
				share_summary: tCollectionData.share_summary,
				share_web: tCollectionData.share_web
			})
			let quickCollection = await db.collection('wmzmall-quick-collection').where({
				shop_id: collectionData.shop_id
			}).get()
			quickCollection = quickCollection.data[0]
			let collection = quickCollection.collection
			for (var i in collection) {
				if (collection[i]._id === id) {
					console.log(collection[i])
					collection[i].share_title = tCollectionData.share_title
					collection[i].share_image = tCollectionData.share_image
					collection[i].share_summary = tCollectionData.share_summary
					collection[i].share_web = tCollectionData.share_web
				}
			}
			await db.collection('wmzmall-quick-collection').doc(quickCollection._id).update({
				collection: collection
			})
		}
	}

	return result_success({
		share_title: tCollectionData.share_title,
		share_image: tCollectionData.share_image,
		share_summary: tCollectionData.share_summary,
		share_web: tCollectionData.share_web,
		share_path: 'pages/collection/collection?collectionId=' + res.id
	})

}